<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Config,Request,Cache,App
};
class UserBank extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['is_default']!='') $map[] = ['ub.is_default','=',$d['is_default']];
            if($d['keyword']!='') $map[] = ['u.title','u.phone',"%{$d['keyword']}%"];
            if($d['min_time']!='') $map[] = ['ub.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['ub.create_time','<=',strtotime($d['max_time'])];
        	$rs = model('UserBank')->alias('ub')
                            ->leftJoin('user u','ub.user_id=u.id')
                            ->field('ub.*,u.username,u.phone')
                            ->where($map)
                            ->order('ub.id desc')
                            ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        
        return $this->fetch();
    }
    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            if($d['is_default']  == 1)	
            	model('UserBank')->default($d['user_id'],$d['id']);

            if(model('UserBank')->renew($d)){
                cookie('js_function','preload');
                return suc();
            }else{
                return err(model('UserBank')->getError());
            }
        }
        $info = model('UserBank')::get($id);
        return $this->fetch('',['info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = input();
            $rs = model('UserBank')->del($d['id']);
            cookie('js_function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('UserBank')->getError());
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
